import os
from google.appengine.ext.webapp import template

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users

from django.utils import simplejson
from business import boplaylist



class MainPage(webapp.RequestHandler):
  '''
  Handler principale dell'applicazione
  '''
  
  def get(self):
    '''
    Metodo GET 
    '''
    self.response.headers['Content-Type'] = 'text/html'
    user = users.get_current_user()
    is_authenticated=False
    if user:
        is_authenticated = True
        greeting = ("Welcome, %s (%s)! (<a href=\"%s\">sign out</a>)" % (user.nickname(), user.user_id(), users.create_logout_url("/")))
    else:
        greeting = ("<a href=\"%s\">Sign in or register</a>." % users.create_login_url("/"))
        
    playlist = False
    if user:
        bp = boplaylist.BoPlaylist()
        playlist = bp.loadUserPlaylist(user)
        
        
    template_values = {
        'saluto': greeting,
        "is_auth": is_authenticated,
        'user': user,
        'playlist': playlist,
    }
    path = os.path.join(os.path.dirname(__file__), '../templates/index.html')
    self.response.out.write(template.render(path, template_values))



def main():
    application = webapp.WSGIApplication([
        ('/', MainPage)
        ],debug=True)
    run_wsgi_app(application)

if __name__ == "__main__":
  main()
